package com.example.list;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class ClassAdapter extends RecyclerView.Adapter<ClassAdapter.MyHolder> {

    List<MyClassBean> datas;
    Context context;
    ItemClickListener mItemClickListener ;

    public ClassAdapter(Context context, List<MyClassBean> datas) {
        this.datas = datas;
        this.context = context;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_class, parent, false);
//        MyHolder holder = new MyHolder(view);
        return new MyHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, final int position) {
        holder.tv_name.setText(datas.get(position).getName());
//        holder.tv_msg.setBackgroundColor(datas.get(position).getColor());

        // 点击事件一般都写在绑定数据这里，当然写到上边的创建布局时候也是可以的
        if (mItemClickListener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 这里利用回调来给RecyclerView设置点击事件
                    mItemClickListener.onItemClick(position,v);
                }
            });
        }

        }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public static class MyHolder extends RecyclerView.ViewHolder {

        TextView tv_name;

        public MyHolder(@NonNull View itemView) {
            super(itemView);

            tv_name = itemView.findViewById(R.id.tv_msg);
        }
    }

    public interface ItemClickListener{
        void onItemClick(int position, View view) ;
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener){
       mItemClickListener = itemClickListener ;

    }

}
